//Early Life Raven
clear
clear matrix
clear mata
cap log close
************************************************************************************
* DIRECTORY ROADMAP
sysdir set PLUS C:\ado\plus\c
************************************************************************************

global dir1 C:\Users\hab55\Dropbox\Second_paper_Ghana\2023_EDCC_Price\Data
global dir2 C:\Users\hab55\Dropbox\Second_paper_Ghana\2023_EDCC_Price\Do
global dir3 C:\Users\hab55\Dropbox\Second_paper_Ghana\2023_EDCC_Price\Output

*******************************************************************************
use "$dir1\cognitive_raven_final.dta", clear  

//////////////////////////////////////////////////////////////////////////////////////////////
///Main
///Table 3: baseline 
///panel A: Early life 

areg raven shock_early_life1 gender head_gender i.ageyears i.ROB i.year, ab (YOB  ) cluster (ROB)
boottest shock_early_life1=0 ,  nograph   

outreg2 using "$dir3\cocoa_cognitive_early.tex", keep(shock_early_life1 ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, No)
////
areg raven shock_early_life1 gender head_gender i.ageyears i.ROB i.year i.ROB#c.YOB , ab (YOB  ) cluster (ROB)
boottest shock_early_life1=0 ,  nograph   

outreg2 using "$dir3\cocoa_cognitive_early.tex", keep(shock_early_life1 ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

////////////////////////////////////////////////////
///Panel B: inutero 
areg raven shock_1 gender head_gender i.ageyears i.ROB i.year, ab (YOB  ) cluster (ROB)
boottest shock_1=0 ,  nograph   

outreg2 using "$dir3\cocoa_cognitive_inutero.tex", keep(shock_1 ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)
/////////////
areg raven shock_1 gender head_gender i.ageyears i.ROB i.year i.ROB#c.YOB , ab (YOB  ) cluster (ROB)
boottest shock_1=0 ,  nograph   

outreg2 using "$dir3\cocoa_cognitive_inutero.tex", keep(shock_1 ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

//////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
///Table 4: IV (international cocoa price)
//second stage 

//Panel A: early life  
//without trend 
ivreg2 raven (shock_early_life1=shock_earlyi1) gender head_gender i.ageyears i.year  i.ROB i.YOB,  cluster (ROB)
boottest shock_early_life1=0 ,  nograph  

gen smpl_1=0
replace smpl_1=1 if e(sample)==1

//outreg2 using "$dir3\second_stage_early.tex", keep(shock_early_life1) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

//with trend 
ivreg2 raven (shock_early_life1=shock_earlyi1) gender head_gender i.ageyears i.year  i.ROB i.YOB i.ROB#c.YOB,  cluster (ROB)
boottest shock_early_life1=0 ,  nograph 


gen smpl_2=0
replace smpl_2=1 if e(sample)==1 
//outreg2 using "$dir3\second_stage_early.tex", keep(shock_early_life1) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)
///////////////////////////////////////////////////////////////////////////////

//Panel B: in-utero   

//without trend 

ivreg2 raven (shock_1=shock_1i) gender head_gender i.ageyears i.year  i.ROB i.YOB,  cluster (ROB)
boottest shock_1=0 ,  nograph  


gen smpl_3=0
replace smpl_3=1 if e(sample)==1

//outreg2 using "$dir3\second_stage_inutero.tex", keep(shock_1) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

//without trend 

ivreg2 raven (shock_1=shock_1i) gender head_gender i.ageyears i.year i.ROB i.YOB i.ROB#c.YOB,  cluster (ROB)
boottest shock_1=0 ,  nograph  


gen smpl_4=0
replace smpl_4=1 if e(sample)==1

//outreg2 using "$dir3\second_stage_inutero.tex", keep(shock_1) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)
/////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//Table B.2 (Panel B)
global des1 "raven ageyears gender head_gender"

keep if raven!=.
eststo A: estpost summarize $des1 

esttab A using "$dir3\Summary_raven.tex", replace  main(mean %6.2f sd ) aux(count)
esttab A using "$dir3\Summary_raven.tex", replace label cell(" mean(fmt(3)) sd(fmt(3)) count(fmt(0))") 
//////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
///////Table C.2: IV (international cocoa price)
///first stage 
areg shock_early_life1 shock_earlyi1  gender head_gender i.ageyears i.year i.YOB if smpl_1==1, ab (ROB)  cluster (ROB)
outreg2 using "$dir3\first_stage_early.tex", keep(shock_earlyi) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

areg shock_early_life1 shock_earlyi1  gender head_gender i.ageyears  i.year i.ROB#c.YOB i.YOB if smpl_2==1, ab (ROB)  cluster (ROB)
outreg2 using "$dir3\first_stage_early.tex", keep(shock_earlyi) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

//panel B
areg shock_1 shock_1i gender head_gender i.ROB#c.YOB  i.YOB if smpl_3==1, ab (ROB) cluster (ROB)
outreg2 using "$dir3\first_stage_inutero.tex", keep(shock_1i) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)


areg shock_1 shock_1i gender head_gender i.ageyears  i.year i.ROB#c.YOB  i.YOB if smpl_4==1, ab (ROB) cluster (ROB)
outreg2 using "$dir3\first_stage_inutero.tex", keep(shock_1i) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)
//////////////////////////////////////////////////////////////////////////////

/////Table C.3: Effects of Raven on other tests 

xi:reg ezread raven gender head_gender years_school rid* i.YOB i.ROB i.year i.ageyears, r
outreg2 using "$dir3\other_cognitive.tex", keep(raven  ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

xi:reg ezmath raven gender head_gender years_school rid* i.YOB i.ROB i.year i.ageyears, r
outreg2 using "$dir3\other_cognitive.tex", keep(raven) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)
/////////////////////////////////////////////////////////////////
keep if take_advread !=.
probit take_advread  ezread ezmath
predict arg_lambda_read, xb
gen phi=normalden(-1*(0.5-arg_lambda_read)) 
gen PHI=normal(-1*(0.5-arg_lambda_read)) 
gen lambda_read=phi / (1-PHI)

xi: reg advread raven  gender  head_gender  lambda_read  rid* i.YOB i.ROB i.year i.ageyears, r
outreg2 using "$dir3\other_cognitive.tex", keep(raven) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)
/////////////////////////////////////////////////////
use "$dir2\cognitive_1989_2003_test_full.dta", clear  
 
keep if take_advmath !=.
probit take_advmath  ezread ezmath
predict arg_lambda_math, xb
gen phi1=normalden(-1*(0.5-arg_lambda_math)) 
gen PHI1=normal(-1*(0.5-arg_lambda_math)) 
gen lambda_math=(phi1) / (1-PHI1)
xi: reg advmath raven  gender  head_gender  lambda_math  rid* i.YOB i.ROB i.year i.ageyears, r
outreg2 using "$dir3\other_cognitive.tex", keep(raven) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)
//////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
////Table D.4: Rainfall control
///panel A

areg raven shock_early_life1 avg_rain_early gender head_gender i.ageyears i.ROB i.year i.ROB#c.YOB, ab (YOB ) cluster (ROB)
boottest shock_early_life1=0 ,  nograph   

outreg2 using "$dir3\rain_cognitive_early.tex", keep(shock_early_life1 avg_rain_early ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

///panel B
areg raven shock_1 avg_rain_1 gender head_gender i.ageyears i.ROB i.year i.ROB#c.YOB, ab (YOB ) cluster (ROB)
boottest shock_1=0 ,  nograph  

outreg2 using "$dir3\rain_cognitive_inutero.tex", keep(shock_1 avg_rain_1 ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

////////////////////////////////////////////////////////////////////////
///Table D.7: houshod fixed effect
/////////////////////////////////////////

xtset housholdid
////panel A
keep if sibsample==1
//if sibsample==1
xi: xtreg raven shock_early_life1 gender  i.YOB rid*   i.year, fe r cluster (housholdid)
boottest shock_early_life1=0 ,  nograph  seed(321448699) 

outreg2 using "$dir3\household_cognitive_earely.tex", keep(shock_early_life1) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes, Interview Year FE, Yes, Household FE, Yes)

////panel B
xi: xtreg raven shock_1 gender   i.YOB rid*  i.year, fe r cluster (housholdid)
boottest shock_1=0 ,  nograph  seed(321448699) 

outreg2 using "$dir3\household_cognitive_inutero.tex", keep(shock_1) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes, Interview Year FE, Yes, Household FE, Yes)

////////////////////////////////////////////////////////////////////////////////
///Table D.9: Intensity
use "$dir1\cognitive_raven_final.dta", clear  

///panel A

areg raven shock_intens_early1 gender head_gender  i.ageyears i.ROB i.ROB#c.YOB i.year, ab (YOB ) cluster (ROB)
boottest shock_intens_early1=0 ,  nograph  seed(321448699) 

outreg2 using "$dir3\shock_intens_early.tex", keep(shock_intens_early1) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

///panel B
areg raven shock_intens gender head_gender i.ageyears i.ROB i.ROB#c.YOB i.year, ab (YOB ) cluster (ROB)
boottest shock_intens=0 ,  nograph  seed(321448699) 

outreg2 using "$dir3\shock_intens_inutero.tex", keep(shock_intens) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)


//////////////////////////////////////////////////////////////
///Table D.11: droping acra and volta

drop if ROB==3
drop if ROB==4
//panel A
areg raven shock_early_life1 gender head_gender  i.ROB i.ROB#c.YOB i.ageyears i.year, ab (YOB ) cluster (ROB)
boottest shock_early_life1=0 ,  nograph  seed(321448699) 

outreg2 using "$dir3\noacranovolta_early.tex", keep(shock_early_life1) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)
///panel B
areg raven shock_1 gender head_gender  i.ROB i.ROB#c.YOB i.ageyears i.year, ab (YOB ) cluster (ROB)
boottest shock_1=0 ,  nograph  seed(321448699) 

outreg2 using "$dir3\noacranovolta_inutero.tex", keep(shock_1) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

///////////////////////////////////////////////////////////////////////////////////
//Table D.13: high low price
use "$dir1\cognitive_raven_final.dta", clear  
///panel A
gen shockboom=High_early*Cocoa_producer
gen shockrecesion=Low_early*Cocoa_producer

gen shocksecond=second*Cocoa_producer
gen shockthird=third*Cocoa_producer

gen shockfourth=fourth*Cocoa_producer

areg raven shockboom shockrecesion gender head_gender i.ageyears i.ROB i.ROB#c.YOB i.year, ab (YOB ) cluster (ROB)

boottest shockboom=0 ,  nograph  seed(321448699) 

boottest shockrecesion=0 ,  nograph  seed(321448699) 
outreg2 using "$dir3\positive_price_cognitive_early_inutero.tex", keep(shockboom shockrecesion) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

areg raven shocksecond shockthird shockfourth gender head_gender i.ageyears i.ROB i.ROB#c.YOB i.year, ab (YOB ) cluster (ROB)

boottest shocksecond=0 ,  nograph  seed(321448699) 

boottest shockthird=0 ,  nograph  seed(321448699)

boottest shockfourth=0 ,  nograph  seed(321448699)

outreg2 using "$dir3\positive_price_cognitive_early_inutero.tex", keep(shocksecond shockthird shockfourth) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

//panel B

gen shockboom1=High_utero*Cocoa_producer
gen shockrecesion1=Low_utero*Cocoa_producer

gen shocksecond_1=second_1*Cocoa_producer
gen shockthird_1=third_1*Cocoa_producer

gen shockfourth_1=fourth_1*Cocoa_producer

areg raven shockboom1 shockrecesion1 gender head_gender  i.ageyears i.ROB i.ROB#c.YOB i.year, ab (YOB ) cluster (ROB)
boottest shockboom1=0 ,  nograph  seed(321448699) 

boottest shockrecesion1=0 ,  nograph  seed(321448699) 
outreg2 using "$dir3\positive_price_cognitive_early_inutero.tex", keep(shockboom1 shockrecesion1 ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)


areg raven shocksecond_1 shockthird_1 shockfourth_1 gender head_gender  i.ageyears i.ROB i.ROB#c.YOB i.year, ab (YOB ) cluster (ROB)
 
 boottest shocksecond_1=0 ,  nograph  seed(321448699) 

boottest shockthird_1=0 ,  nograph  seed(321448699)

boottest shockfourth_1=0 ,  nograph  seed(321448699) 
outreg2 using "$dir3\positive_price_cognitive_early_inutero.tex", keep(shocksecond_1 shockthird_1 shockfourth_1 ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)


//////////////////////////////////////////////////////////////////////////
//Table D.16: gender hetrogeniety
///Panel A
use "$dir2\cognitive_1989_2003_test_full.dta", clear

areg raven shock_early_life1 gender head_gender  i.ROB#c.YOB i.ROB i.ageyears i.year if gender==1, ab (YOB  ) cluster (ROB)
boottest shock_early_life1=0 ,  nograph  seed(321448699) 

outreg2 using "$dir3\cocoa_cognitive_gender_early.tex", keep(shock_early_life1 ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

use "$dir2\cognitive_1989_2003_test_full.dta", clear

areg raven shock_early_life1 gender head_gender  i.ROB#c.YOB i.ROB i.ageyears i.year if gender==0, ab (YOB  ) cluster (ROB)
boottest shock_early_life1=0 ,  nograph  seed(321448699) 

outreg2 using "$dir3\cocoa_cognitive_gender_early.tex", keep(shock_early_life1 ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)
////////////////////////////////////////////////////////////////////////////////
///Panel B
use "$dir2\cognitive_1989_2003_test_full.dta", clear

areg raven shock_1 gender head_gender  i.ROB#c.YOB i.ROB if gender==1, ab (YOB  ) cluster (ROB)
boottest shock_1=0 ,  nograph  seed(321448699) 

outreg2 using "$dir3\cocoa_cognitive_gender_inutero.tex", keep(shock_1 ) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)

use "$dir2\cognitive_1989_2003_test_full.dta", clear

areg raven shock_1 gender head_gender  i.ROB#c.YOB i.ROB if gender==0, ab (YOB  ) cluster (ROB)
boottest shock_1=0 ,  nograph  seed(321448699) 

outreg2 using "$dir3\cocoa_cognitive_gender_inutero.tex", keep(shock_1) stats(coef se) bdec(3) sdec(3) nocons  label addtext(ROB FE, Yes, YOB FE, Yes, ROB Trends, Yes)
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////



